﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using System.Collections;

namespace School.Service.BOOKS
{
    public static class SRV_BOOKS_TRANSACTIONS
    {
        private static SchoolEntities db = new SchoolEntities();
        public static IEnumerable<SRV_BOOK_STOCKTACKING> SRV_Books_STOCKTACKINGAll()
        {
            IEnumerable<SRV_BOOK_STOCKTACKING> query = from stocks in db.SRV_BOOK_STOCKTACKING
                                                          select stocks;
            return query.GroupBy(s => s.Date_Time).Select(s => s.FirstOrDefault());
        }
        public static bool SRV_BOOKS_TRANACTIONSInsert(List<SRV_BOOK_TRANSACTION> trans)
        {
            foreach (SRV_BOOK_TRANSACTION item in trans)
            {
                db.AddToSRV_BOOK_TRANSACTION(item);
                db.SRV_Book.Where(s => s.Id == item.Book_Id).FirstOrDefault().BookQty += item.Qty;
            }

            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IEnumerable<object> SRV_Books_STOCKTACKINGSearch(DateTime date)
        {
            var query = from stock in db.SRV_BOOK_STOCKTACKING
                        where stock.Date_Time == date
                        select new
                        {
                            BookId = stock.Book_Id,
                            BookCode = stock.SRV_Book.Code,
                            BookName = stock.SRV_Book.Name,
                            SystemQty = stock.System_Qty,
                            Pysical_Qty = stock.Pysical_Qty,
                            Differance = stock.Pysical_Qty - stock.System_Qty,
                            Equating_Date_Time = stock.Equating_Date_Time != null ? (DateTime)stock.Equating_Date_Time : (DateTime?)null
                        };
            return query;
        }
        public static bool SRV_TRANSACTIONInsertByCode(Hashtable table)
        {
            foreach (DictionaryEntry item in table)
            {
                if (db.SRV_Book.Where(s => s.Code == (string)item.Key).FirstOrDefault() == null)
                    return false;
                db.AddToSRV_BOOK_TRANSACTION(new SRV_BOOK_TRANSACTION()
                {
                    Book_Id = db.SRV_Book.Where(s => s.Code == (string)item.Key).First().Id,
                    Qty = int.Parse(item.Value.ToString()),
                    Transaction_Type = 1,
                    Date_Time = DateTime.Now
                });
                db.SRV_Book.Where(s => s.Code == (string)item.Key).First().BookQty += int.Parse(item.Value.ToString());
            }

            //if (lstSizes.Count < 1)
            //    return false;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool SRV_Books_TRANSACTIONInsert(SRV_BOOK_TRANSACTION trans, out long trans_Id)
        {

            db.AddToSRV_BOOK_TRANSACTION(trans);
            try
            {
                db.SaveChanges();
                trans_Id = trans.Trans_Id;
                return true;
            }
            catch
            {
                trans_Id = 0;
                return false;
            }
        }
    }
}
